1
|
|
|
var ExtensibleData = require('./ExtensibleData'), |
2
|
|
|
Person = require('./Person'), |
3
|
|
|
Relationship = require('./Relationship'), |
4
|
|
|
SourceDescription = require('./SourceDescription'), |
5
|
|
|
Agent = require('./Agent'), |
6
|
|
|
Event = require('./Event'), |
7
|
|
|
Document = require('./Document'), |
8
|
|
|
PlaceDescription = require('./PlaceDescription'), |
9
|
|
|
Attribution = require('./Attribution'), |
10
|
|
|
utils = require('./utils'); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A GEDCOM X document. |
14
|
|
|
* |
15
|
|
|
* @constructor |
16
|
|
|
* @param {Object} [json] |
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
var GedcomX = function(json){ |
19
|
|
|
|
20
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
21
|
|
|
if(!(this instanceof GedcomX)){ |
22
|
|
|
return new GedcomX(json); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
26
|
|
|
if(GedcomX.isInstance(json)){ |
27
|
|
|
return json; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
ExtensibleData.call(this, json); |
31
|
|
|
|
32
|
|
|
if(json){ |
|
|
|
|
33
|
|
|
this.setLang(json.lang); |
34
|
|
|
this.setPersons(json.persons); |
35
|
|
|
this.setRelationships(json.relationships); |
36
|
|
|
this.setSourceDescriptions(json.sourceDescriptions); |
37
|
|
|
this.setAgents(json.agents); |
38
|
|
|
this.setEvents(json.events); |
39
|
|
|
this.setDocuments(json.documents); |
40
|
|
|
this.setPlaces(json.places); |
41
|
|
|
this.setAttribution(json.attribution); |
42
|
|
|
this.setDescription(json.description); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
GedcomX.prototype = Object.create(ExtensibleData.prototype); |
47
|
|
|
|
48
|
|
|
GedcomX._gedxClass = GedcomX.prototype._gedxClass = 'GedcomX'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check whether the given object is an instance of this class. |
52
|
|
|
* |
53
|
|
|
* @param {Object} obj |
54
|
|
|
* @returns {Boolean} |
55
|
|
|
*/ |
56
|
|
|
GedcomX.isInstance = function(obj){ |
57
|
|
|
return utils.isInstance(obj, this._gedxClass); |
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the lang |
62
|
|
|
* |
63
|
|
|
* @return {String} |
64
|
|
|
*/ |
65
|
|
|
GedcomX.prototype.getLang = function(){ |
66
|
|
|
return this.lang; |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the lang |
71
|
|
|
* |
72
|
|
|
* @param {String} lang |
73
|
|
|
* @return {GedcomX} This instance |
74
|
|
|
*/ |
75
|
|
|
GedcomX.prototype.setLang = function(lang){ |
76
|
|
|
if(lang){ |
77
|
|
|
this.lang = lang; |
78
|
|
|
} |
79
|
|
|
return this; |
80
|
|
|
}; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the description |
84
|
|
|
* |
85
|
|
|
* @return {String} |
86
|
|
|
*/ |
87
|
|
|
GedcomX.prototype.getDescription = function(){ |
88
|
|
|
return this.description; |
89
|
|
|
}; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the description |
93
|
|
|
* |
94
|
|
|
* @param {String} description URI that must resolve to a SourceDescription |
95
|
|
|
* @return {GedcomX} This instance |
96
|
|
|
*/ |
97
|
|
|
GedcomX.prototype.setDescription = function(description){ |
98
|
|
|
if(description){ |
99
|
|
|
this.description = description; |
100
|
|
|
} |
101
|
|
|
return this; |
102
|
|
|
}; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the persons |
106
|
|
|
* |
107
|
|
|
* @returns {Person[]} |
108
|
|
|
*/ |
109
|
|
|
GedcomX.prototype.getPersons = function(){ |
110
|
|
|
return this.persons || []; |
111
|
|
|
}; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the persons |
115
|
|
|
* |
116
|
|
|
* @param {Person[]|Object[]} persons |
117
|
|
|
* @returns {GedcomX} This instance |
118
|
|
|
*/ |
119
|
|
|
GedcomX.prototype.setPersons = function(persons){ |
120
|
|
|
return this._setArray(persons, 'persons', 'addPerson'); |
121
|
|
|
}; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Add a person |
125
|
|
|
* |
126
|
|
|
* @param {Person|Object} |
|
|
|
|
127
|
|
|
* @returns {GedcomX} This instance |
128
|
|
|
*/ |
129
|
|
|
GedcomX.prototype.addPerson = function(person){ |
130
|
|
|
return this._arrayPush(person, 'persons', Person); |
131
|
|
|
}; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the relationships |
135
|
|
|
* |
136
|
|
|
* @returns {Relationship[]} |
137
|
|
|
*/ |
138
|
|
|
GedcomX.prototype.getRelationships = function(){ |
139
|
|
|
return this.relationships || []; |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the relationships |
144
|
|
|
* |
145
|
|
|
* @param {Relationship[]|Object[]} relationships |
146
|
|
|
* @returns {GedcomX} |
147
|
|
|
*/ |
148
|
|
|
GedcomX.prototype.setRelationships = function(relationships){ |
149
|
|
|
return this._setArray(relationships, 'relationships', 'addRelationship'); |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add a relationship |
154
|
|
|
* |
155
|
|
|
* @param {Relationship|Object} relationship |
156
|
|
|
* @returns {GedcomX} |
157
|
|
|
*/ |
158
|
|
|
GedcomX.prototype.addRelationship = function(relationship){ |
159
|
|
|
return this._arrayPush(relationship, 'relationships', Relationship); |
160
|
|
|
}; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the source descriptions |
164
|
|
|
* |
165
|
|
|
* @returns {SourceDescription[]} |
166
|
|
|
*/ |
167
|
|
|
GedcomX.prototype.getSourceDescriptions = function(){ |
168
|
|
|
return this.sourceDescriptions || []; |
169
|
|
|
}; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set the source descriptions |
173
|
|
|
* |
174
|
|
|
* @param {SourceDescription[]|Object[]} sourceDescriptions |
175
|
|
|
* @returns {GedcomX} |
176
|
|
|
*/ |
177
|
|
|
GedcomX.prototype.setSourceDescriptions = function(sourceDescriptions){ |
178
|
|
|
return this._setArray(sourceDescriptions, 'sourceDescriptions', 'addSourceDescription'); |
179
|
|
|
}; |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Add a ource description |
183
|
|
|
* |
184
|
|
|
* @param {SourceDescription|Object} sourceDescription |
185
|
|
|
* @returns {GedcomX} |
186
|
|
|
*/ |
187
|
|
|
GedcomX.prototype.addSourceDescription = function(sourceDescription){ |
188
|
|
|
return this._arrayPush(sourceDescription, 'sourceDescriptions', SourceDescription); |
189
|
|
|
}; |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the agents |
193
|
|
|
* |
194
|
|
|
* @returns {Agent[]} |
195
|
|
|
*/ |
196
|
|
|
GedcomX.prototype.getAgents = function(){ |
197
|
|
|
return this.agents || []; |
198
|
|
|
}; |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Set the agents |
202
|
|
|
* |
203
|
|
|
* @param {Agent[]|Object[]} agents |
204
|
|
|
* @returns {GedcomX} |
205
|
|
|
*/ |
206
|
|
|
GedcomX.prototype.setAgents = function(agents){ |
207
|
|
|
return this._setArray(agents, 'agents', 'addAgent'); |
208
|
|
|
}; |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Add an agent |
212
|
|
|
* |
213
|
|
|
* @param {Agent|Object} |
|
|
|
|
214
|
|
|
* @returns {GedcomX} |
215
|
|
|
*/ |
216
|
|
|
GedcomX.prototype.addAgent = function(agent){ |
217
|
|
|
return this._arrayPush(agent, 'agents', Agent); |
218
|
|
|
}; |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get events |
222
|
|
|
* |
223
|
|
|
* @returns {Event[]} |
224
|
|
|
*/ |
225
|
|
|
GedcomX.prototype.getEvents = function(){ |
226
|
|
|
return this.events || []; |
227
|
|
|
}; |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set events |
231
|
|
|
* |
232
|
|
|
* @param {Event[]|Object[]} events |
233
|
|
|
* @returns {GedcomX} |
234
|
|
|
*/ |
235
|
|
|
GedcomX.prototype.setEvents = function(events){ |
236
|
|
|
return this._setArray(events, 'events', 'addEvent'); |
237
|
|
|
}; |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Add an event |
241
|
|
|
* |
242
|
|
|
* @param {Event|Object} event |
243
|
|
|
* @returns {GedcomX} |
244
|
|
|
*/ |
245
|
|
|
GedcomX.prototype.addEvent = function(event){ |
246
|
|
|
return this._arrayPush(event, 'events', Event); |
247
|
|
|
}; |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get the documents |
251
|
|
|
* |
252
|
|
|
* @returns {Document[]} |
253
|
|
|
*/ |
254
|
|
|
GedcomX.prototype.getDocuments = function(){ |
255
|
|
|
return this.documents || []; |
256
|
|
|
}; |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Set the documents |
260
|
|
|
* |
261
|
|
|
* @param {Documents[]|Object[]} documents |
262
|
|
|
* @returns {GedcomX} |
263
|
|
|
*/ |
264
|
|
|
GedcomX.prototype.setDocuments = function(documents){ |
265
|
|
|
return this._setArray(documents, 'documents', 'addDocument'); |
266
|
|
|
}; |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Add a document |
270
|
|
|
* |
271
|
|
|
* @param {Document|Object} doc |
272
|
|
|
* @returns {GedcomX} |
273
|
|
|
*/ |
274
|
|
|
GedcomX.prototype.addDocument = function(doc){ |
275
|
|
|
return this._arrayPush(doc, 'documents', Document); |
276
|
|
|
}; |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get places |
280
|
|
|
* |
281
|
|
|
* @returns {PlaceDescription[]} |
282
|
|
|
*/ |
283
|
|
|
GedcomX.prototype.getPlaces = function(){ |
284
|
|
|
return this.places || []; |
285
|
|
|
}; |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Set the places |
289
|
|
|
* |
290
|
|
|
* @param {PlaceDescription[]|Object} places |
291
|
|
|
* @returns {GedcomX} |
292
|
|
|
*/ |
293
|
|
|
GedcomX.prototype.setPlaces = function(places){ |
294
|
|
|
return this._setArray(places, 'places', 'addPlace'); |
295
|
|
|
}; |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Add a place |
299
|
|
|
* |
300
|
|
|
* @param {PlaceDescription} place |
301
|
|
|
* @returns {GedcomX} |
302
|
|
|
*/ |
303
|
|
|
GedcomX.prototype.addPlace = function(place){ |
304
|
|
|
return this._arrayPush(place, 'places', PlaceDescription); |
305
|
|
|
}; |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Get attritbution |
309
|
|
|
* |
310
|
|
|
* @returns {Attribution} |
311
|
|
|
*/ |
312
|
|
|
GedcomX.prototype.getAttribution = function(){ |
313
|
|
|
return this.attribution; |
314
|
|
|
}; |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Set attribution |
318
|
|
|
* |
319
|
|
|
* @param {Attribution} attribution |
320
|
|
|
* @returns {GedcomX} |
321
|
|
|
*/ |
322
|
|
|
GedcomX.prototype.setAttribution = function(attribution){ |
323
|
|
|
if(attribution){ |
324
|
|
|
this.attribution = Attribution(attribution); |
325
|
|
|
} |
326
|
|
|
return this; |
327
|
|
|
}; |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Export the object as JSON |
331
|
|
|
* |
332
|
|
|
* @return {Object} JSON object |
333
|
|
|
*/ |
334
|
|
|
GedcomX.prototype.toJSON = function(){ |
335
|
|
|
return this._toJSON(ExtensibleData, [ |
336
|
|
|
'lang', |
337
|
|
|
'description', |
338
|
|
|
'persons', |
339
|
|
|
'relationships', |
340
|
|
|
'sourceDescriptions', |
341
|
|
|
'agents', |
342
|
|
|
'events', |
343
|
|
|
'documents', |
344
|
|
|
'places', |
345
|
|
|
'attribution' |
346
|
|
|
]); |
347
|
|
|
}; |
348
|
|
|
|
349
|
|
|
// Expose all classes |
350
|
|
|
GedcomX.Address = require('./Address'); |
351
|
|
|
GedcomX.Agent = Agent; |
352
|
|
|
GedcomX.Attribution = Attribution; |
353
|
|
|
GedcomX.Conclusion = require('./Conclusion'); |
354
|
|
|
GedcomX.Coverage = require('./Coverage'); |
355
|
|
|
GedcomX.Date = require('./Date'); |
356
|
|
|
GedcomX.Document = Document; |
357
|
|
|
GedcomX.Event = Event; |
358
|
|
|
GedcomX.EventRole = require('./EventRole'); |
359
|
|
|
GedcomX.EvidenceReference = require('./EvidenceReference'); |
360
|
|
|
GedcomX.ExtensibleData = ExtensibleData; |
361
|
|
|
GedcomX.Fact = require('./Fact'); |
362
|
|
|
GedcomX.Gender = require('./Gender'); |
363
|
|
|
GedcomX.Identifiers = require('./Identifiers'); |
364
|
|
|
GedcomX.Name = require('./Name'); |
365
|
|
|
GedcomX.NameForm = require('./NameForm'); |
366
|
|
|
GedcomX.NamePart = require('./NamePart'); |
367
|
|
|
GedcomX.Note = require('./Note'); |
368
|
|
|
GedcomX.OnlineAccount = require('./OnlineAccount'); |
369
|
|
|
GedcomX.Person = Person; |
370
|
|
|
GedcomX.PlaceDescription = PlaceDescription; |
371
|
|
|
GedcomX.PlaceReference = require('./PlaceReference'); |
372
|
|
|
GedcomX.Qualifier = require('./Qualifier'); |
373
|
|
|
GedcomX.Relationship = Relationship; |
374
|
|
|
GedcomX.ResourceReference = require('./ResourceReference'); |
375
|
|
|
GedcomX.SourceCitation = require('./SourceCitation'); |
376
|
|
|
GedcomX.SourceDescription = SourceDescription; |
377
|
|
|
GedcomX.SourceReference = require('./SourceReference'); |
378
|
|
|
GedcomX.Subject = require('./Subject'); |
379
|
|
|
GedcomX.TextValue = require('./TextValue'); |
380
|
|
|
|
381
|
|
|
module.exports = GedcomX; |